package com.dms.interceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

public class AuthorizedInterceptor implements HandlerInterceptor {

	//定义不需要拦截的请求//
	private static final String[] IGNORE_URI = {"/dms.web", "/login","/404","/500","/index","/taglib"};
	
	
	public void afterCompletion(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, Exception arg3)
			throws Exception {
		// TODO Auto-generated method stub
		
	}

	public void postHandle(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, ModelAndView arg3)
			throws Exception {
		
	}

	//设置拦截器，未登录直接跳转到登陆页面
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
	   
		//判断请求是否需要拦截
		String servletpathString=request.getServletPath();
		for (String s : IGNORE_URI) {
			if(servletpathString.contains(s)){
				return true;
			}
		}
		
		String ad_username =  (String)request.getSession().getAttribute("ad_username");   
	    String username =  (String)request.getSession().getAttribute("username");   
        if(ad_username == null&&username == null){  	           
           request.setAttribute("message", "请先登录再访问网站!");
   		   request.getRequestDispatcher("/index").forward(request, response);
            return false;  
        }else  
            return true;  
	}
}
